﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Text;
using System.IO;
namespace System
{
    public static class MD5
    {
        /// <summary>
        /// 得到字符串的MD5码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetStringMD5(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }

        /// <summary>
        /// 得到文件的md5码
        /// </summary>
        /// <param name="destinationFile"></param>
        /// <returns></returns>
        public static string ReadMD5(this string filePath)
        {
            if (System.IO.File.Exists(filePath))
            {
                System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] hash_byte = get_md5.ComputeHash(fs);
                fs.Flush();
                fs.Close();
                return BitConverter.ToString(hash_byte).Replace("-", "");
            }
            else
            {
                return "";
            }
        }
    }
}
